/**
 * Role数据传输对象
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.dto;


import com.sxf.crm.core.entity.Role;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色数据传输对象
 * 
 * <p>用于角色相关的数据传输</p>
 * 
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/6/1
 */
@Data
public class RoleDTO extends Role {
    private Long id;

    private String name;

    private String code;

    private String description;

    private Integer status;

    private Set<Long> permissionIds;

    private LocalDateTime createTime;

    private LocalDateTime updateTime;

    public static RoleDTO fromEntity(Role role) {
        if (role == null) {
            return null;
        }
        RoleDTO dto = new RoleDTO();
        BeanUtils.copyProperties(role, dto);
        
        // 转换权限ID列表
        if (role.getPermissions() != null) {
            dto.setPermissionIds(role.getPermissions().stream()
                .map(permission -> permission.getId())
                .collect(Collectors.toSet()));
        }
        
        return dto;
    }
}
